package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private SpuService spuService;

    @Autowired
    private SkuService skuService;

    @Autowired
    private SpuDetailService detailService;

    /**
     * 商品分页查询
     * @param page
     * @param rows
     * @param id
     * @param cid
     * @param bid
     * @param saleable
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> pageQuery(
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "rows",defaultValue = "5")Integer rows,
            @RequestParam(value = "id",required = false)Long id,
            @RequestParam(value = "categoryId",required = false)Long cid,
            @RequestParam(value = "brandId",required = false)Long bid,
            @RequestParam(value = "saleable",required = false)Boolean saleable){

        return ResponseEntity.ok(this.spuService.pageQuery(page,rows,id,cid,bid,saleable));
    }


    /**
     * 商品新增
     * @param spuDTO
     * @return
     */
    @PostMapping(value = "/spu")
    public ResponseEntity<Void> addGoods(
            @RequestBody SpuDTO spuDTO){

        this.spuService.addGoods(spuDTO);

        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    /**
     * 根据spuId查询商品信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> queryGoodsById(@PathVariable("id")Long id){

        return ResponseEntity.ok(this.spuService.queryGoodsById(id));
    }


    /**
     * 商品新增
     * @param spuDTO
     * @return
     */
    @PutMapping(value = "/spu")
    public ResponseEntity<Void> updateGoods(
            @RequestBody SpuDTO spuDTO){

        this.spuService.updateGoods(spuDTO);

        return ResponseEntity.ok().build();
    }

    /**
     * 修改商品的上下架状态
     * @param id
     * @param saleable
     * @return
     */
    @PutMapping("/saleable")
    public ResponseEntity<Void> modifySaleable(
            @RequestParam("id")Long id,
            @RequestParam("saleable")Boolean saleable){

        this.spuService.modifySaleable(id,saleable);
        return ResponseEntity.ok().build();
    }

    /**
     * 根据spuId查询spu
     * @param id
     * @return
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id")Long id){

        return ResponseEntity.ok(this.spuService.querySpuById(id));
    }


    /**
     * 根据sku的id集合查询sku集合
     * @param ids
     * @return
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> listSkuByIds(
            @RequestParam("ids")List<Long> ids){
        return ResponseEntity.ok(this.skuService.listSkuByIds(ids));
    }


    /**
     * 根据spuId查询对应的sku的集合
     * @param spuId
     * @return
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> listSkuBySpu(
            @RequestParam("id")Long spuId){

        return ResponseEntity.ok(this.skuService.listSkuBySpu(spuId));
    }

    /**
     * 根据spuId查询对应的spuDetail
     * @param id
     * @return
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailById(
            @RequestParam("id")Long id){
        return ResponseEntity.ok(this.detailService.querySpuDetailById(id));
    }

    /**
     * 根据商品id或是否可搜索查询对应的规格参数
     * @param id
     * @param searching
     * @return
     */
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> queryParam(
            @RequestParam("id")Long id,
            @RequestParam(value = "searching",required = false)Boolean searching){

        return ResponseEntity.ok(this.spuService.queryParam(id,searching));
    }


    @PutMapping("/stock/minus")
    public ResponseEntity<Void> minusStock(
            @RequestBody Map<Long ,Integer> cartsMap){
        this.skuService.minusStock(cartsMap);
        return ResponseEntity.ok().build();
    }
}
